/*
 * this will sets footer tool bar. and all the click event for the each footer button are define in this class.
 * also the select and un select of menu will be set here in the globaldata class.
 */
package com.fipl.taksar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ToolBar {

	public static final int DESHBOARD_ID = 100;
	public static final int VARIABLE_ID = 101;
	public static final int HISTORICAL_ID = 102;
	public static final int CONFIGURATION_ID = 103;

	//button references
	public static ImageView deshboardImageView;
	public static ImageView variableImageView;
	public static ImageView historicalImageView;
	public static ImageView configurationImageView;
	public static Activity currentActivity;


	public static void setup(Activity a){
		currentActivity = a;
		deshboardImageView = (ImageView) a.findViewById(DESHBOARD_ID);
		variableImageView = (ImageView) a.findViewById(VARIABLE_ID);
		historicalImageView = (ImageView) a.findViewById(HISTORICAL_ID);
		configurationImageView = (ImageView) a.findViewById(CONFIGURATION_ID);
	}

	public static void setNewFocus(int focus_id){
		switch (focus_id) {
		case 0:
			deshboardImageView.requestFocus();
			break;
		case 1:
			variableImageView.requestFocus();
			break;
		case 2:
			historicalImageView.requestFocus();
			break;
		case 3:
			configurationImageView.requestFocus();
			break;
		}
	}

	public static class ToolBarView extends TableLayout {

		
		private ImageView deshboardImageView;
		private ImageView variableImageView;
		private ImageView historicalImageView;
		private ImageView configurationImageView;
		private LinearLayout linearLayout1;
		private LinearLayout linearLayout2;
		private LinearLayout linearLayout3;
		private LinearLayout linearLayout4;
		private Context context;
		
		public ToolBarView(final Context context, AttributeSet attrs) {
			super(context, attrs);
			this.context=context;

			// Create row container
			TableRow row = new TableRow(context);
			row.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

			// Create button using Image View
			deshboardImageView = new ImageView(context);
			variableImageView = new ImageView(context);
			historicalImageView = new ImageView(context);
			configurationImageView = new ImageView(context);

			// set layoutparams for image view of the footer
			deshboardImageView.setLayoutParams(new Gallery.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			variableImageView.setLayoutParams(new Gallery.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			historicalImageView.setLayoutParams(new Gallery.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			configurationImageView.setLayoutParams(new Gallery.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

			TextView deshboardTextView = new TextView(context);
			deshboardTextView.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			deshboardTextView.setText(R.string.deshboard);
			deshboardTextView.setTextSize(8);
			deshboardTextView.setGravity(Gravity.CENTER);

			TextView variableTextView = new TextView(context);
			variableTextView.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			variableTextView.setText(R.string.variable);
			variableTextView.setTextSize(8);
			variableTextView.setGravity(Gravity.CENTER);

			TextView historicalTextView = new TextView(context);
			historicalTextView.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			historicalTextView.setText(R.string.historical);
			historicalTextView.setTextSize(8);
			historicalTextView.setGravity(Gravity.CENTER);

			TextView configurationTextView = new TextView(context);
			configurationTextView.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			configurationTextView.setText(R.string.configuration);
			configurationTextView.setTextSize(8);
			configurationTextView.setGravity(Gravity.CENTER);

			deshboardImageView.setId(DESHBOARD_ID);
			variableImageView.setId(VARIABLE_ID);
			historicalImageView.setId(HISTORICAL_ID);
			configurationImageView.setId(CONFIGURATION_ID);

			// Set Padding l,t,r,b
			deshboardImageView.setPadding(0, 0, 0, 0);
			variableImageView.setPadding(0, 0, 0, 0);
			historicalImageView.setPadding(0, 0, 0, 0);
			configurationImageView.setPadding(0, 0, 0, 0);

			// Set Background Image
			switch (((GlobalData)context.getApplicationContext()).getMenuNo()) {
			case 100:
				deshboardImageView.setBackgroundResource(R.drawable.deshboardselect);
				variableImageView.setBackgroundResource(R.drawable.vairable);
				historicalImageView.setBackgroundResource(R.drawable.historical);
				configurationImageView.setBackgroundResource(R.drawable.configration);
				break;
			case 101:
				deshboardImageView.setBackgroundResource(R.drawable.deshboard);
				variableImageView.setBackgroundResource(R.drawable.vairableselect);
				historicalImageView.setBackgroundResource(R.drawable.historical);
				configurationImageView.setBackgroundResource(R.drawable.configration);
				break;
			case 102:
				deshboardImageView.setBackgroundResource(R.drawable.deshboard);
				variableImageView.setBackgroundResource(R.drawable.vairable);
				historicalImageView.setBackgroundResource(R.drawable.historicalselect);
				configurationImageView.setBackgroundResource(R.drawable.configration);
				break;
			case 103:
				deshboardImageView.setBackgroundResource(R.drawable.deshboard);
				variableImageView.setBackgroundResource(R.drawable.vairable);
				historicalImageView.setBackgroundResource(R.drawable.historical);
				configurationImageView.setBackgroundResource(R.drawable.configrationselect);
				break;
			default:
				deshboardImageView.setBackgroundResource(R.drawable.deshboardselect);
				variableImageView.setBackgroundResource(R.drawable.vairable);
				historicalImageView.setBackgroundResource(R.drawable.historical);
				configurationImageView.setBackgroundResource(R.drawable.configration);
				break;
			}
			
			//Linear layout for all image button and textview of the footer
			linearLayout1 = new LinearLayout(context);
			linearLayout1.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			linearLayout1.setOrientation(LinearLayout.VERTICAL);
			linearLayout1.setGravity(Gravity.CENTER);
			linearLayout1.setBackgroundColor(Color.BLACK);

			linearLayout2 = new LinearLayout(context);
			linearLayout2.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			linearLayout2.setOrientation(LinearLayout.VERTICAL);
			linearLayout2.setGravity(Gravity.CENTER);
			linearLayout2.setBackgroundColor(Color.BLACK);

			linearLayout3 = new LinearLayout(context);
			linearLayout3.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			linearLayout3.setOrientation(LinearLayout.VERTICAL);
			linearLayout3.setGravity(Gravity.CENTER);
			linearLayout3.setBackgroundColor(Color.BLACK);

			linearLayout4 = new LinearLayout(context);
			linearLayout4.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			linearLayout4.setOrientation(LinearLayout.VERTICAL);
			linearLayout4.setGravity(Gravity.CENTER);
			linearLayout4.setBackgroundColor(Color.BLACK);

			// button image and text view is added in the linear layout
			linearLayout1.addView(deshboardImageView);
			linearLayout1.addView(deshboardTextView);
			linearLayout2.addView(variableImageView);
			linearLayout2.addView(variableTextView);
			linearLayout3.addView(historicalImageView);
			linearLayout3.addView(historicalTextView);
			linearLayout4.addView(configurationImageView);
			linearLayout4.addView(configurationTextView);

			// Add buttons to row
			row.addView(linearLayout1, new TableRow.LayoutParams(
					android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			row.addView(linearLayout2, new TableRow.LayoutParams(
					android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			row.addView(linearLayout3, new TableRow.LayoutParams(
					android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			row.addView(linearLayout4, new TableRow.LayoutParams(
					android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

			// Make columns expand to fill space
			setColumnStretchable(0, true);
			setColumnStretchable(1, true);
			setColumnStretchable(2, true);
			setColumnStretchable(3, true);

			// Add row to table
			addView(row, new TableLayout.LayoutParams(
					android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

			deshboardImageView.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					setMenuNumber(100);
					Intent i = new Intent(context,SelectVariable.class);
					context.startActivity(i);
				}
			});

			variableImageView.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					setMenuNumber(101);
					Intent i = new Intent(context, Variable2Activity.class);
					context.startActivity(i);
				}
			});

			historicalImageView.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					setMenuNumber(102);
					Intent i = new Intent(context, HistoricalActivity.class);
					context.startActivity(i);
				}
			});

			configurationImageView.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					setMenuNumber(103);
					Intent i = new Intent(context,ConfigurationActivity.class);
					context.startActivity(i);
				}
			});			

		}
		// this will be set menu number in to the globaldata class.
		public void setMenuNumber(int menuNo){
			((GlobalData)context.getApplicationContext()).setMenuNo(menuNo);
		}

	}
	
}
